<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use App\Models\District;
class Store extends Model
{
    //
    protected $table = 'store';
    protected $guarded = ['id'];


    public function visit($upid)
    {
       return $this->where('upid',$upid)->get();

    }

    public function storeGet()
    {
        $data = $this->get();
        if(empty($data)){
            return null;
        }
        $list = [];
        $i = 0;
        foreach ($data as $k=>$v){

            $list[$i]['id'] = $v->id;
            $list[$i]['name'] = $v->name;
            $list[$i]['username'] = $v->username;
            $list[$i]['status'] = $v->status;
            $list[$i]['logo'] = $v->logo;
            $address = $v->address;
            $city = explode(',',$v->path);
            array_pop($city);

            $head = '';
            $foot = '';

            foreach ($city as $v) {
                $name = District::find($v)->name;
                $head .= "[{'text':'{$name}','nodes':";
                $foot .= "}]";

            }

            $foot .= "}]";
            $head .= "[{'text':'{$address}'";
            //$str= rtrim($head.$foot,",nodes':}]");
            $str=$head.$foot;
            $list[$i]['path'] = $str;

            $i++;
        }

        return json_encode($list);
    }

}
